/**
 * 随机获取颜色
 * @date 2021-07-20
 * @returns {String} 样式
 */
export const getRandomColor = () => {
  const r = Math.floor(Math.random() * 256)
  const g = Math.floor(Math.random() * 256)
  const b = Math.floor(Math.random() * 256)
  return 'rgb(' + r + ',' + g + ',' + b + ')'
}

/**
 * 16进制色值转rgb
 * @date 2021-07-26
 * @param {String} hex
 * @returns {String}
 */
export const hexToRgba = (hex) => {
  return (
    'rgb(' +
    parseInt('0x' + hex.slice(1, 3)) +
    ',' +
    parseInt('0x' + hex.slice(3, 5)) +
    ',' +
    parseInt('0x' + hex.slice(5, 7)) +
    ')'
  )
}

/**
 * 去掉rgb
 * @date 2021-07-30
 * @param {String} a
 * @returns {String}
 */
export const getColorRgb = (a) => {
  if (a && a.length > 0) {
    a = a.replace('rgb(', '').replace(')', '')
  } else {
    a = null
  }
  return a
}

/**
 * rgb色值转16进制
 * @date 2021-07-26
 * @param {String} color
 * @returns {String}
 */
export const rgbToHex = (color) => {
  const rgb = color.split(',')
  const r = parseInt(rgb[0].split('(')[1])
  const g = parseInt(rgb[1])
  const b = parseInt(rgb[2].split(')')[0])
  const hex =
    '#' + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1)
  return hex
}

/**
 * 遍布选中节点周围的点坐标
 * @date 2021-07-23
 * @param {中心节点} centerNode
 * @param {节点数量} nodeCount
 * @param {距离中心点距离} raduis
 * @returns {Array}
 */
export const getXY = (centerNode, nodeCount, raduis) => {
  const aop = 360.0 / nodeCount
  const arr = []
  for (let i = 0; i < nodeCount; i++) {
    let r1 = raduis
    if (nodeCount > 10) {
      r1 = i % 2 === 0 ? raduis + 35 : raduis - 35
    }
    const ao = i * aop
    const o1 = {}
    o1.x = centerNode.x + r1 * Math.cos((ao * Math.PI) / 180)
    o1.y = centerNode.y + r1 * Math.sin((ao * Math.PI) / 180)
    arr[i] = o1
  }
  return arr
}

/**
 * 随机产生节点
 * @date 2021-07-23
 * @param {当前选中节点} centerNode
 * @param {节点数量} nodeNum
 * @returns {Object}
 */
export const buildIncreamData = (centerNode, nodeNum) => {
  const gxData = {
    nodes: [],
    links: []
  }
  const count = nodeNum
  const nodeIdPrefix = 'node_' + Math.round(Math.random() * 1000) + '_'
  const color = getRandomColor()
  for (let i = 0; i < count; i++) {
    gxData.nodes.push({
      id: nodeIdPrefix + i,
      label: '子节点+' + i,
      size: 60,
      color
    })
    gxData.links.push({
      source: centerNode.id,
      target: nodeIdPrefix + i,
      label: '关系' + i
    })
  }
  return gxData
}
